//
//  CellForCT.m
//  HYHOME
//
//  Created by Mac on 16/1/26.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "CellForCT.h"
#import "UIImageView+WebCache.h"
#import "ModelImage.h"

#import "CellOfcollection1.h"
#import "DKNightVersion.h"

@interface CellForCT ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property (nonatomic,retain)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,retain)UICollectionView *collectionV;
@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic,retain)NSMutableArray *arrpic;
@end
@implementation CellForCT



- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self handle];
        //    [self createTableView];
        
        [self createCollectionView];
    }
    
    return self;
}



- (void)handle{
    
    
    NSURL *url =[ NSURL URLWithString:@"http://yyjd.xinzhuzhang.com/m/jiaju/goods/category/list?appid=881376723&channel=AppleStore2&token=&udid=852f2d86c0414900d4b72ef95c09ccf6d39d87c2&idfa=EEACF35A-5B73-4678-B6EF-E188A26B0232&idfv=E60577C5-F045-4F8D-99F2-A98F40DF00AB&mac=02:00:00:00:00:00&timestamp=1453682278.158729&v=1.2.0&ios=9.1&sign=df26779064d872dfbe9b86276d889a08"];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            self.arrpic = [NSMutableArray array];
            self.array = [NSMutableArray array];
            //self.arrkt = [NSMutableArray array];
            
            NSError *errorJson = nil;
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorJson];
            
            
            NSArray *arr = [result objectForKey:@"catItemList"];
            
            NSDictionary *dic = [arr objectAtIndex:2];
            
            NSArray *arr2 = [dic objectForKey:@"catlist"];
            for (NSDictionary  *dic2 in arr2) {
                
                ModelImage *model =[[ ModelImage alloc]init];
                model.picUrl = [dic2 objectForKey:@"picUrl"];
                model.catname = [dic2 objectForKey:@"catname"];
                
                model.catid = [dic2 objectForKey:@"catid"];

                [self.array addObject:model];
                [model release];
                
                
            }
            
         
            
            [self.collectionV reloadData];
            
        });
    }];
    
    
    [task resume];
    
}


- (void)createCollectionView{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake((self.frame.size.width - 80)/2.0, ((self.frame.size.width - 80)/ 2.0)*1.618);
    
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.flowLayout.footerReferenceSize = CGSizeMake(0, 70);

    
    self.collectionV = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowLayout];
    
    [self.contentView addSubview:self.collectionV];
    
    self.collectionV.dataSource = self;
    self.collectionV.delegate = self;
    
    [self.collectionV registerClass:[CellOfcollection1 class] forCellWithReuseIdentifier:@"pool3"];
    
  
    self.collectionV.dk_backgroundColorPicker = DKColorWithRGB(0xF5F5FF, 0x343434);

    
    
    
    self.collectionV.showsHorizontalScrollIndicator = NO;
    [self.collectionV release];
    [self.flowLayout release];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return    self.array.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    CellOfcollection1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool3" forIndexPath:indexPath];
    
    ModelImage *model = [self.array objectAtIndex:indexPath.row ];
    
    [cell passModel:model];
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if ([self.delegate respondsToSelector:@selector(didClickItem:)]) {
        [self.delegate didClickItem:[self.array objectAtIndex:indexPath.item]];
    }
    
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{

    
    
    
}

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    
    return layoutAttributes;
    
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
}




@end
